package com.xlt.activiti.group;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.net.ServerSocket;

public class ActivitiGroup {

    private static ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    private static TaskService taskService = processEngine.getTaskService();

    public static String taskAssigneeSearch (String key,String userId){
        Task task = taskService.createTaskQuery().processDefinitionKey(key).taskAssignee(userId).singleResult();

        if (task!=null){
            System.out.println("assginee:" + task.getAssignee());
            System.out.println("processInstanceId:" + task.getProcessInstanceId());
            return task.getId();
        }
        return null;
    }

    public static String taskACandidateUserSearch (String key,String userId){
        Task task = taskService.createTaskQuery().processDefinitionKey(key).taskCandidateUser(userId).singleResult();

        if (task!=null){
            System.out.println("assginee:" + task.getAssignee());
            System.out.println("processInstanceId:" + task.getProcessInstanceId());
            return task.getId();
        }
        return null;
    }

    public static void taskChaim(String key,String userId){
        Task task = taskService.createTaskQuery().processDefinitionKey(key).taskCandidateUser(userId).singleResult();
        if (task!=null)
        {
            taskService.claim(task.getId(),userId);
        }
    }

    public static void taskUnChaim(String key,String userId){
        Task task = taskService.createTaskQuery().processDefinitionKey(key).taskAssignee(userId).singleResult();
        if (task!=null){
            taskService.setAssignee(task.getId(),null);
        }
    }

    public static void completeTask(String key,String userId){
        Task task = taskService.createTaskQuery().processDefinitionKey(key).taskAssignee(userId).singleResult();
        if(task!=null){
            taskService.complete(task.getId());
        }
    }
}
